package com.api.oa.controller;


import com.api.oa.model.entity.OaOrg;
import com.api.oa.services.IOaOrgService;
import com.api.shopapi.controller.BaseController;
import com.api.shopapi.entity.MyResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/oa/org")
public class OaOrgController extends BaseController {

    @Autowired
    private IOaOrgService iOaOrgService;

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public MyResult Add(@RequestBody OaOrg org) {
        try {
            return iOaOrgService.add(org) == 1 ? success(org) : failed("操作失败");
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }

    }


    @RequestMapping(value = "/remove/{id}", method = RequestMethod.POST)
    public MyResult remove(@PathVariable Long id) {
        try {
            iOaOrgService.remove(id);
            return success("删除成功!");
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }

    }


    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public MyResult update(@RequestBody OaOrg org) {
        try {
            return iOaOrgService.update(org) == 1 ?  success("操作成功") : failed("操作失败");
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }

    }


    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public MyResult list(@RequestParam(required = false) Long pid) {
        try {
            return success(iOaOrgService.list(pid));
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }
    }


    @RequestMapping(value = "/one/{id}", method = RequestMethod.GET)
    public MyResult one(@PathVariable Long id) {
        try {
            return success(id == null ? null :iOaOrgService.one(id));
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }
    }


}
